package com.gzxx.util;

import com.gzxx.security.CurrentUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.List;

/**
 * 系统当前登录用户帮助类
 * @Author pengpdx
 * @Date 2020/7/28 15:46
 */
public class SysLoginUserHelper {

    /**
     * 获取认证令牌信息
     * @return
     */
    public static Authentication getCurrentUserAuthentication(){
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取当前用户
     * @return
     */
    public static CurrentUser getCurrentPrincipal(){
        return (CurrentUser)getCurrentUserAuthentication().getPrincipal();
    }

    /**
     * 获取用户ID
     * @return
     */
    public static Long getLoginUserId() {
        CurrentUser currentUser = getCurrentPrincipal();
        if(currentUser != null) {
            return currentUser.getId();
        }
        return null;
    }

    /**
     * 获取登录用户名
     * @return
     */
    public static String getLoginUserName() {
        CurrentUser currentUser = getCurrentPrincipal();
        if(currentUser != null) {
            return currentUser.getUsername();
        }
        return null;
    }

    /**
     * 获取登录用户权限
     * @return
     */
    public static List<String> getLoginUserPermissions(){
        CurrentUser currentUser = getCurrentPrincipal();
        if(currentUser != null) {
            return currentUser.getPermissions();
        }
        return null;
    }



}
